package com.zm.tools.generatebackcode;

import com.zm.tools.generatebackcode.core.GenerateCodeUtil;
import com.zm.tools.util.core.TableMetadateReadUtil;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import org.springframework.core.io.FileSystemResource;

import java.io.File;
import java.util.Arrays;

/**
 * @author zoum
 * @create 2019/1/29 17:27
 */
@Mojo(name="generatebackcode", defaultPhase = LifecyclePhase.PACKAGE)
public class Generate extends AbstractMojo {

    /**

     * @throws MojoExecutionException
     * @throws MojoFailureException
     */
    @Parameter
    String configFilePath;

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {

        File file = new File(configFilePath);
        if (!file.exists()){
            return;
        }

        PropertiesConfigUtil propertiesConfigUtil = new PropertiesConfigUtil();
        propertiesConfigUtil.setResources(Arrays.asList(new FileSystemResource(file)));

        TableMetadateReadUtil tableMetadateReadUtil = new TableMetadateReadUtil();
        tableMetadateReadUtil.setDriverClassName(propertiesConfigUtil.getPropByKey("driver"));
        tableMetadateReadUtil.setUrl(propertiesConfigUtil.getPropByKey("url"));
        tableMetadateReadUtil.setUsername(propertiesConfigUtil.getPropByKey("username"));
        tableMetadateReadUtil.setPassword(propertiesConfigUtil.getPropByKey("password"));
        GenerateCodeUtil generateCodeUtil = new GenerateCodeUtil();
        generateCodeUtil.setTableMetadateReadUtil(tableMetadateReadUtil);

        String[] tableNames = propertiesConfigUtil.getPropByKey("tableNames").split(",");
        String baseFilePath = propertiesConfigUtil.getPropByKey("baseFilePath");
        String companyName = propertiesConfigUtil.getPropByKey("companyName");
        String appName = propertiesConfigUtil.getPropByKey("appName");
        try {
            generateCodeUtil.generateCode(baseFilePath, companyName, appName, tableNames);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
